import 'package:fish_redux/fish_redux.dart';
import 'action.dart';
import 'state.dart';
import '../../../app_files.dart';
import 'package:flutter/material.dart' hide Action;

Effect<WebViewState> buildEffect() {
  return combineEffects(<Object, Effect<WebViewState>>{
    Lifecycle.initState: _init,
    Lifecycle.dispose: _dispose,
    WebViewAction.onBack: _onBack,
    WebViewAction.onSearch: _onSearch,
  });
}

void _init(Action action, Context<WebViewState> ctx) {
  bus.on('webviewRefresh', (arg) {
    ctx.dispatch(WebViewActionCreator.onRefresh());
  });
}

void _onBack(Action action, Context<WebViewState> ctx) {
  if (!ctx.state.webInfo.canGoBack) {
    Navigator.of(ctx.context).pop(true);
    return;
  }
  ctx.state.webViewController.canGoBack().then((canBack) {
    if (canBack) {
      ctx.state.webViewController.goBack();
    } else {
      Navigator.of(ctx.context).pop(true);
    }
  });
}

void _onSearch(Action action, Context<WebViewState> ctx) {
//  CommonUtils.pushWebViewFlutterPage(ctx.context, null, WebUrlConfig.searchUrl);
}

void _dispose(Action action, Context<WebViewState> ctx) {}
